package com.itheima;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

/**
 * @auther Macrydy
 * @date 2020/7/10 14:14
 */
public class TcpServer {
    public static void main(String[] args) throws IOException {

        while (true) {
            //创建服务器端的Socket对象(ServerSocket)
            //ServerSocket(int port) 创建绑定到指定端口的服务器套接字
            ServerSocket ss = new ServerSocket(10300);

            //Socket accept() 侦听要连接到此套接字并接受它
            Socket accept = ss.accept();
            //获取输入流，读数据
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            InputStream is = accept.getInputStream();
            //创建要字节缓冲输出流，把上传的文件写到指定位置
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day14Internet/copy/" + UUID.randomUUID().toString() + ".jpg"));

            int len;
            while ((len=bis.read())!=-1){
                bos.write(len);
            }
            bos.flush();

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("下载成功");
            bw.flush();
            bw.close();

            //释放资源
            is.close();
            accept.close();
        }
        //ss.close();
    }
}
